package com.aisile.admin.service.impl;

import com.aisile.admin.mapper.AdSensitiveMapper;
import com.aisile.admin.service.AdSensitiveService;
import com.aisile.model.admin.dtos.SensitiveDto;
import com.aisile.model.admin.pojos.AdSensitive;
import com.aisile.model.common.dtos.PageResponseResult;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam();

        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }

        IPage<AdSensitive> iPage = this.page(new Page<>(dto.getPage(), dto.getSize()), wrapper);

        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),Integer.parseInt(iPage.getTotal()+""));
        responseResult.setData(iPage);
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if(adSensitive == null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.PARAM_INVALID);
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AdminHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive == null || adSensitive.getId() == null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.PARAM_INVALID);
        }

        updateById(adSensitive);
        return ResponseResult.okResult(AdminHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.PARAM_INVALID);
        }

        AdSensitive adSensitive = getById(id);
        if (adSensitive == null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult(AdminHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findById(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive = getById(id);
        if (adSensitive == null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.DATA_NOT_EXIST);
        }
        this.getById(id);
        return ResponseResult.okResult(adSensitive);
    }
}
